*** BCG baseline
* Bustein, Carvalho and Grassi




****************************** 1. Preamble ******

* clean up 
	clear all 
	macro drop _all
	set max_memory 50g
	set matsize 11000

*set working directory. Must contain:
	cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package"
	

****************************************CHANGE SPECS AND OUTLIER ***************	
local specsDeta_l   _baseline
local winlevel_l    03   
********************************************************************************


	di "===============Quick control================="
	di "specsDeta_l=`specsDeta_l'"
	di "winlevel_l=`winlevel_l'"	
	
	**********************CHOICE of outliuer treatment level
	* winsorization level 
	global winlevel_gl `winlevel_l'
	di "winlevel_gl=$winlevel_gl"

	*trimmeing level (thousands)
	if "`winlevel_l'"=="01"{
		global toptr_gl 990
		global bottr_gl 10
	}
	else if "`winlevel_l'"=="015"{
		global toptr_gl 985
		global bottr_gl 15
	}
	else if "`winlevel_l'"=="02"{
		global toptr_gl 980
		global bottr_gl 20
	}
	else if "`winlevel_l'"=="025"{
		global toptr_gl 975
		global bottr_gl 25
	}
	else if "`winlevel_l'"=="03"{
		global toptr_gl 970
		global bottr_gl 30
	}
	
	di "toptr_gl = $toptr_gl"
	di "toptr_gl = $bottr_gl"

		
	*Select Specification
	global specsDeta `specsDeta_l'
	di "specsDeta (global)==$specsDeta"	
	
	di "results ending in  _$winlevel_gl$specsDeta" 
	
	
	

	* select dataset with production data 
	global dataset "ficusfare_reduced_9419_sec2_v0222"
	local indlist 08 13 14 15 16 17 18 20 22 23 24 25 26 27 28 29 30 31 32 33 43 46 70 95


	
	
	di "data=$dataset"
	
	di "==================================="
		

******************************************************************************************
****************************** 2. Load the data ******
******************************************************************************************
	
*select path
cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package"

*load the firm-level data:
	
	
	use data/$dataset, clear 
	
	**drop some useless variables
	capture drop acha1 acha2 acha3 acha5 acha6 cogs cogs_newdef acha1R acha2R acha3R acha5R acha6R  invcorp catotal_tot

	**destring the sector code before the merge
	destring naf2d, replace

	
*Compute Markup 

	** define production variables
	gen s = ln(catotalR)
	gen m = ln(acha4R)	//m will be the variable input 
	gen v = ln(salR)
	gen k = ln(immocorR)
	gen o = ln(autachaR)

	** define cost ratio 
	gen ratio = catotal/acha4 
	
	**Loop over the quantity
	foreach specs in FSQ {
		***merge coefficients 
		merge m:1 naf2d using "data\coefficients_byind_Python_`specs'$specsDeta.dta", nogen
		
		***rename coefficients 
		foreach beta in const_cd m_cd v_cd o_cd k_cd const_tl m_tl v_tl o_tl k_tl m2_tl mv_tl mo_tl mk_tl v2_tl vo_tl vk_tl o2_tl ok_tl k2_tl {
			rename beta_`beta' beta_`beta'_`specs'
		}
		
		***drop redundant variables
		drop N med_CD med_TL iqr_CD iqr_TL  rho_cd  rho_tl 
		
		
		***calculate elasticity - TL 
		gen elast_tl_s`specs'_t1 = beta_m_tl_`specs'	 				 	 + 	/// 
													2*beta_m2_tl_`specs'*m   + 	///
													  beta_mv_tl_`specs'*v 	 +	///
													  beta_mo_tl_`specs'*o 	 +	///
													  beta_mk_tl_`specs'*k	 
												
		***define elasticity - CD 									
		gen elast_cd_s`specs'_t1 = beta_m_cd_`specs'
		
		
		***calculate markups 
		gen mu_sepcal_TL_s`specs'_t1 =  elast_tl_s`specs'_t1 * ratio 
		gen mu_sepcal_CD_s`specs'_t1 =  elast_cd_s`specs'_t1 * ratio
			
		***calculate log markups
		gen l_mu_sepcal_TL_s`specs'_t1 = log(mu_sepcal_TL_s`specs'_t1) 
		gen l_mu_sepcal_CD_s`specs'_t1 = log(mu_sepcal_CD_s`specs'_t1)
	}
	
	


**drop some useless variables
capture drop ml_* ratio_* elast_*	
	


************************************************************************************************************************************************************************************
******************************************************************* 3. Selecting Final Sample and Some indicators
************************************************************************************************************************************************************************************	
	
*Keep firms with positive VA	
	keep if va>0

*Keep firms with positive catotal and inputs
	keep if sample_firm_inputs==1

*Keep the relevent sectors
	keep if sample_sectors2==1 //should be no change
	keep if naf2d_num != 95 & naf2d_num != 70 //Two other sectors that we drop
	

*Some indicator of markup level (quantity)
	gen markup_pos = 0
	replace markup_pos = 1 if mu_sepcal_TL_sFSQ_t1 > 0



*Keep siren with more than one year
	bysort siren: egen count_siren=count(catotal)
	keep  if count_siren>1
	
	
*Construct Sector-Level VA

	** Sector-level sum of va
	bysort naf_single year: egen va_total = total(va) 
	gen l_va_total = ln(va_total)
	
		
	**Get the list of 5-digits sector to drop
	local Nperiods 26
	preserve
		
		duplicates drop naf_single year , force 	
		keep naf_single year va_total  l_va_total 
		
		bysort naf_single: egen count_naf=count(l_va_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va.dta, replace
		
	restore
	
	
	
*Indicator of sector with complete VA series
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d
	gen sample_sector_5d = 1
	foreach naf_sec in 13.96Z 16.22Z 24.46Z 27.31Z 27.33Z 28.24Z 28.99A 30.12Z 31.09A 33.11Z 33.13Z 33.17Z 33.19Z 33.20B 33.20D 43.11Z 43.21B 46.12A 46.47Z 46.48Z 46.65Z 46.73B{
		replace sample_sector_5d = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va.dta"
	gen sample_sector_5d_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d sample_sector_5d_v2
	keep if sample_sector_5d_v2==1
	
	*Get the list of 5-digits sector to drop for positive markup firms
	local Nperiods 26
	preserve
		
		** Sector-level sum of va for positive markup firms
		gen va_pos = .
		replace va_pos = va if markup_pos==1
		
		bysort naf_single year: egen va_pos_total = total(va_pos) 
		gen l_va_pos_total = ln(va_pos_total)
		
		su va_pos va l_va_pos_total l_va_total
		
		** make it a sector level database
		duplicates drop naf_single year , force 	
		keep naf_single year va_pos_total  l_va_pos_total 
		
		bysort naf_single: egen count_naf=count(l_va_pos_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va_pos.dta, replace
		
	restore //one sector to drop beyond the one dropped previously
	
*Indicator of sector with complete VA series for positive markup sample
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d_pos
	gen sample_sector_5d_pos = 1
	foreach naf_sec in 30.30Z{
		replace sample_sector_5d_pos = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va_pos.dta"
	gen sample_sector_5d_pos_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d_pos sample_sector_5d_pos_v2
	keep if sample_sector_5d_pos_v2==1
	

*Aggregate-level sum of va
	**For the full sample
	bysort year: egen va_agg = total(va)
	gen l_va_agg = ln(va_agg)
	
		

************************************************************************************************************************************************************************************
******************************************************************* 4. Construct Regression Variables
************************************************************************************************************************************************************************************	
		
		
* Sector-Level VA (at naf_single level)
local Nperiods 26
preserve
	
	**Make the data a sector-level panel
	duplicates drop naf_single year , force 	
	keep naf_single year va_total l_va_total 

	**set up panel
	egen naf_single_num = group(naf_single)
	xtset naf_single_num year
	
	**fix gaps (hp filter does not run with gaps but now it should no be any of these gaps)
	drop if l_va_total == .  //0 changes
	bysort naf_single: drop if _N < `Nperiods' //0 changes

	** generate HP-filtered data 
	tsfilter hp x = l_va_total, smooth(6.25) trend(l_va_total_hp)
	drop x
	gen l_va_total_hp_dev = l_va_total - l_va_total_hp
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_total, frequency(yearly) stub(l_va_total_ha) 
	gen l_va_total_ha_dev = l_va_total - l_va_total_ha_trend
	
	** generate First-Difference-filtered data 
	xtset naf_single_num year
	gen  l_va_total_FD = l_va_total - L.l_va_total
	
	** generate Demean data 
	by naf_single_num: egen l_va_total_mean = mean(l_va_total)
	gen  l_va_total_demean = l_va_total - l_va_total_mean	

	* save results 
	save BCG/data/l_va_total_hp_$specsDeta, replace

restore


* Aggregate-Level VA 
preserve
	**Make the data a yearly time serie
	duplicates drop year , force 	
	keep year va_agg l_va_agg  
	**Set up time serie
	tset year 
	
	**demean aggregate GDP
	egen l_va_agg_mean = mean(l_va_agg)
	gen l_va_agg_demean = l_va_agg - l_va_agg_mean
	
	**FD of GDP
	gen FD_l_va_agg = l_va_agg - L.l_va_agg

	**generate HP-filtered data for full sample
	tsfilter hp x = l_va_agg, smooth(6.25) trend(l_va_agg_hp)
	drop x
	gen l_va_agg_hp_dev = l_va_agg - l_va_agg_hp
	
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_agg, frequency(yearly) stub(l_va_agg_ha) 
	gen l_va_agg_ha_dev = l_va_agg - l_va_agg_ha_trend
	
	
	**save results 
	save BCG/data/l_va_agg_hp_$specsDeta, replace

restore

*Merging with sector-level HP-filtered data for all firms
	merge m:1 naf_single year using "BCG/data/l_va_total_hp_$specsDeta.dta" 
	drop _merge 
	
*Merging with aggregate level HP-filtered data
	merge m:1 year using "BCG/data/l_va_agg_hp_$specsDeta.dta" 
	drop _merge 


*Market Share
	
	**Sector level revenue
	bysort naf_single year: egen catotal_total = total(catotal)
	
	**Market Share
	gen share5 = catotal/catotal_total
	
	**Some summary stats
	su share5  


*Interaction market share with sector output	
	**HP-Deviation
		***Interaction market share * sector output: (market share * HP-filtered (log) sector-level VA)
			gen l_va_total_hp_d_inter = l_va_total_hp_dev*share5	 
			
		
	** Hamilton-filter
		***Interaction market share * sector output: (market share * HA-filtered (log) sector-level VA)
			gen l_va_total_ha_d_inter = l_va_total_ha_dev*share5	 
	
	**First-Difference (log)
		***Interaction market share * sector output: (market share * FD(log) sector-level VA)
			gen l_va_total_FD_d_inter = l_va_total_FD*share5	 
			
	
	**Demeaned
		***Interaction market share * sector output: (market share * Demean (log) sector-level VA)
			gen l_va_total_demean_d_inter = l_va_total_demean*share5	 
			
	
	
*Revenue Share in total revenue
	
	**Yearly total revenue
	bysort  year: egen catotal_aggr = total(catotal)
	
	**Revenue Share
	gen revShare = catotal/catotal_aggr
	
	**Some summary stats
	su revShare  

	
	

*Inverse markup
	xtset firmsId year

	**level
	gen inv_mu_sepcal_TL_sFSQ_t1 = 1/mu_sepcal_TL_sFSQ_t1
			
	**in first-diff			
	gen FD_inv_mu_sepcal_TL_sFSQ_t1  = inv_mu_sepcal_TL_sFSQ_t1 - L.inv_mu_sepcal_TL_sFSQ_t1
	


*Compute number of firms in each 5digit sectors
	bysort naf_single year: egen firmcount_naf_va=count(va)
	bysort naf_single year: egen firmcount_naf_ca=count(catotal)
	bysort naf_single year: egen firmcount_naf_mu=count(mu_sepcal_TL_sFSQ_t1)
	su firmcount_naf_*
	
	*since all the same let us keep only one
	drop firmcount_naf_ca firmcount_naf_mu
	
	
	

	
	

************************************************************************************************************************************************************************************
******************************************************************* 5. Firm-Level Regressions
************************************************************************************************************************************************************************************	


		
		
*************** 5.1 inverse markup on market share 


	**Winsorized inverse markup
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
		
		gen mu_sepcal_TL_sFSQ_t1_pos = .
		replace mu_sepcal_TL_sFSQ_t1_pos = mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		gen inv_mu_sepcal_TL_sFSQ_t1_pos = .
		replace inv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		
		winsor  inv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(inv_mu_sepcal_TL_sFSQ_t1_pos_w)
		
	
	**Some summarry stats
		su inv_mu_sepcal_TL_sFSQ_t1_pos_w  share5 if markup_pos==1
		
		
	**Regression
		
		***In level

			****no FE
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w  share5  if markup_pos==1 & year>1994, noabsorb cl(year firmsId) 
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , replace tex   keep( share5 ) addtext(Firm FE, No, Year FE, No, Cluster , Firm Year)


			**** with Year FE
			*reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5 i.year if markup_pos==1& year>1994, noabsorb cl(year firmsId)	
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5 if markup_pos==1& year>1994, absorb(year) cl(year firmsId)	
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , append tex   keep( share5 ) addtext(Firm FE, No, Year FE, Yes, Cluster , Firm Year)
			
			***With market*year FE
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5  if markup_pos==1 & year>1994, absorb(year#naf_single_num) cl(year firmsId)
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , append tex   keep( share5 ) addtext(Firm FE, Yes, Year FE, No , Sector*Year FE, Yes, Cluster , Firm Year)

			**** with Firm FE
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5 if markup_pos==1& year>1994, absorb(firmsId) cl(year firmsId)
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , append tex   keep( share5 ) addtext(Firm FE, Yes, Year FE, No, Cluster , Firm Year)
			
			**** with Year and Firm FE
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5 i.year if markup_pos==1 & year>1994, absorb(firmsId) cl(year firmsId)
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , append tex   keep( share5 ) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Firm Year)
			

			***With market*year and firm FE
			reghdfe inv_mu_sepcal_TL_sFSQ_t1_pos_w share5  if markup_pos==1 & year>1994, absorb(firmsId year#naf_single_num) cl(year firmsId) tech(lsqr)
			outreg2 using BCG\results_sl\MarkupMShare_level_$winlevel_gl$specsDeta , append tex   keep( share5 ) addtext(Firm FE, No, Year FE, No, Sector*Year FE, Yes, Cluster , Firm Year)
			
		

		****In first-difference
		
				******set up panel
				xtset firmsId year
				
				******Compute First-Difference
				gen FDinv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1_pos - L.inv_mu_sepcal_TL_sFSQ_t1_pos
				gen FDshare5 = share5 - L.share5
				
				local top_tr $toptr_gl
				local bot_tr $bottr_gl
				local winlevel $winlevel_gl
								
				winsor  FDinv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(FDinv_mu_sepcal_TL_sFSQ_t1_pos_w)


				**Some summarry stats
				su FDinv_mu_sepcal_TL_sFSQ_t1_pos_w FDshare5 if markup_pos==1
									
					
				
				*****Regressions
					*with no FE
					reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w  FDshare5  if markup_pos==1 & year>1994, noabsorb cl(year firmsId) 
					outreg2 using BCG\results_sl\MarkupMShare_FD_$winlevel_gl$specsDeta , replace tex   keep( FDshare5 ) addtext(Firm FE, No, Year FE, No, Cluster , Firm Year)
									
					
					*with year FE
					*reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w FDshare5 i.year if markup_pos==1& year>1994, noabsorb cl(year firmsId)	
					reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w FDshare5  if markup_pos==1& year>1994, absorb(year) cl(year firmsId)	
					outreg2 using BCG\results_sl\MarkupMShare_FD_$winlevel_gl$specsDeta , append tex   keep( FDshare5 ) addtext(Firm FE, No, Year FE, Yes, Cluster , Firm Year)
							


					**with market*year FE
					reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w  FDshare5  if markup_pos==1 & year>1994, absorb(year#naf_single_num) cl(year firmsId)  tech(lsqr)
					outreg2 using BCG\results_sl\MarkupMShare_FD_$winlevel_gl$specsDeta , append tex   keep( FDshare5 ) addtext(Firm FE, No, Year FE, No, Year*Market FE, Yes, Cluster , Firm Year)
						
					**Counts firms
					distinct firmsId if markup_pos==1 & year>1994 & FDinv_mu_sepcal_TL_sFSQ_t1_pos_w~=. &   FDshare5 ~= .
	

*************** 5.2 Firm markup on sector output   
	**Sorting the data
		xtset firmsId year

		
	**Taking First-Difference
		gen FD_l_mu_sepcal_TL_sFSQ_t1 = l_mu_sepcal_TL_sFSQ_t1 - L.l_mu_sepcal_TL_sFSQ_t1

	**Winsorized the LHS
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
				
		winsor  l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(l_mu_sepcal_TL_sFSQ_t1_w)
		winsor  FD_l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(FD_l_mu_sepcal_TL_sFSQ_t1_w)
		
	**Some summarry stats
		su l_mu_sepcal_TL_sFSQ_t1_w l_va_total_hp_dev l_va_total_hp_d_inter share5 if markup_pos==1
		
		su l_mu_sepcal_TL_sFSQ_t1_w l_va_total_ha_dev l_va_total_ha_d_inter share5 if markup_pos==1
		
		su l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter share5 if markup_pos==1
		
		su l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_demean  l_va_total_demean_d_inter  share5 if markup_pos==1
		
		su FD_l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter share5 if markup_pos==1
		
	
	
	**Regressions
		**Demeaned (baseline 1)
			reghdfe l_mu_sepcal_TL_sFSQ_t1_w   l_va_total_demean  l_va_total_demean_d_inter i.year if markup_pos==1 & year>1994 , absorb(firmsId)  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , replace tex   keep( l_va_total_demean  l_va_total_demean_d_inter) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector * Year Filter, Demeaned)
	
			*** with a large_firm indicator
				gen large_firm=0
				replace large_firm=1 if share5>=0.5
				
				gen l_va_total_demean_d_inter_Large = l_va_total_demean*large_firm
				
				reghdfe l_mu_sepcal_TL_sFSQ_t1_w   l_va_total_demean  l_va_total_demean_d_inter_Large  i.year if markup_pos==1 & year>1994 , absorb(firmsId)  cl(naf_single_num#year)
				outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep( l_va_total_demean  l_va_total_demean_d_inter_Large ) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector * Year, Filter, Demeaned)

	
		**FD (baseline 2)
			reghdfe FD_l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter  if markup_pos==1 & year>1994 , noabsorb  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep( l_va_total_FD l_va_total_FD_d_inter) addtext(Firm FE, No, Year FE, No, Cluster , Sector * Year, Filter, FD)
			
			***with large irm indicator
				gen l_va_total_FD_d_inter_Large = l_va_total_demean*large_firm
				
				reghdfe FD_l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter_Large  if markup_pos==1 & year>1994 , noabsorb  cl(naf_single_num#year)
				outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep( l_va_total_FD l_va_total_FD_d_inter_Large ) addtext(Firm FE, No, Year FE, No, Cluster , Sector * Year, Filter, FD)
			
		**Hamilton Filter		
			reghdfe l_mu_sepcal_TL_sFSQ_t1_w l_va_total_ha_dev l_va_total_ha_d_inter i.year if markup_pos==1 & year>1994 , absorb(firmsId)  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev l_va_total_ha_d_inter) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector * Year, Filter, Hamilton)
			
		**Hp-Fitler
			reghdfe l_mu_sepcal_TL_sFSQ_t1_w l_va_total_hp_dev l_va_total_hp_d_inter i.year if markup_pos==1 & year>1994 , absorb(firmsId)  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev l_va_total_hp_d_inter) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector * Year,  Filter, HP)
			
			
	
	
					
*************** 5.3 Market Share Cyclicality

	***** 5.3.1 Market Share

		*Computed average market share
			**Compute average firm-level market share over full sample
			bysort firmsId: egen share5_m = mean(share5)
			su share5_m, d 
			
			**Compute log of market share
			 gen l_share5 = log(share5)



			*Market Share Log****************	
				*** Demeaned (baseline)
				
				
					***All firms
					reghdfe l_share5 l_va_total_demean i.year if markup_pos==1 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(l_va_total_demean) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year, Filter, Demeaned)
					
				
					***Large firms
					reghdfe l_share5 l_va_total_demean  i.year if markup_pos==1 & share5_m>0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year) 
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_demean) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year,  Firms, Large>0.5,  Filter, Demeaned)
					
					***Small firms
					reghdfe l_share5 l_va_total_demean  i.year if markup_pos==1 & share5_m<=0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_demean) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year,  Firms, Small<0.5,  Filter, Demeaned)
					
				*** FD (baseline)*
				xtset firmsId year
				capture gen FDl_share5 = l_share5 -L.l_share5
				
				local top_tr $toptr_gl
				local bot_tr $bottr_gl
				local winlevel $winlevel_gl
						
				winsor  FDl_share5 , p(.`winlevel') gen(FDl_share5_w)
				
				
					***All firms
					reghdfe FDl_share5_w l_va_total_FD  if markup_pos==1 & year>1994, noabsorb  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year, Firms, All, Filter, FD)
				
					distinct firmsId if markup_pos==1  & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .
				
					***Large firms
					reghdfe FDl_share5_w l_va_total_FD  if markup_pos==1 & share5_m>0.5 & year>1994, noabsorb  cl(naf_single_num#year) 
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year,  Firms, Large>0.5,  Filter, FD)
					
					distinct firmsId if markup_pos==1 & share5_m>0.5 & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .
					
					***Small firms
					reghdfe FDl_share5_w l_va_total_FD if markup_pos==1 & share5_m<=0.5 & year>1994, noabsorb  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year,   Firms, Small<0.5, Filter, FD)
					
					distinct firmsId if markup_pos==1 & share5_m<=0.5 & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .	
				
				*** with Hamilton (robustness)
				capture gen l_share5 = log(share5)
				
					***All firms
					reghdfe l_share5 l_va_total_ha_dev i.year if markup_pos==1 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year, Firms, All, Filter, Demeaned)
					
					***Large firms
					reghdfe l_share5 l_va_total_ha_dev  i.year if markup_pos==1 & share5_m>0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year) 
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year, Firms, Large>0.5, Filter, Demeaned)
					
					***Small firms
					reghdfe l_share5 l_va_total_ha_dev  i.year if markup_pos==1 & share5_m<=0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year,  Firms, Small<0.5,  Filter, Demeaned)
				
				*** with HP (robustness)
				capture gen l_share5 = log(share5)
				
					***All firms
					reghdfe l_share5 l_va_total_hp_dev i.year if markup_pos==1 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year,Firms, All, Filter, Demeaned)
					
					***Large firms
					reghdfe l_share5 l_va_total_hp_dev  i.year if markup_pos==1 & share5_m>0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year) 
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year, Firms, Large>0.5,  Filter, Demeaned)
					
					***Small firms
					reghdfe l_share5 l_va_total_hp_dev  i.year if markup_pos==1 & share5_m<=0.5 & year>1994, absorb(firmsId)  cl(naf_single_num#year)
					outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev) addtext(Firm FE, Yes, Year FE, Yes, Cluster , Sector x Year,  Firms, Small<0.5, Filter, Demeaned)
						
					

			
					
					
***************************************************************************************************************************************************
******************************************************************* 6. Sector-Level Regressions
***************************************************************************************************************************************************

*************** 6.1 Compute the sector-level and aggregate markup

*Sector-level markup based on winsorized inverse markup
			
		bysort year naf_single: egen inv_mu_TL_sFSQ_t1_pos_w_h = wtmean(inv_mu_sepcal_TL_sFSQ_t1_pos_w), weight( share5 )
		
		gen mu_TL_sFSQ_t1_pos_w_h = 1/inv_mu_TL_sFSQ_t1_pos_w_h
		gen  l_mu_TL_sFSQ_t1_pos_w_h = log(mu_TL_sFSQ_t1_pos_w_h)


		
*Aggregate-level markup	based on winsorized inverse markup
	

				
		*With winsorized inverse markup	and market share on full sample	
		
		bysort year : egen inv_mu_TL_sFSQ_t1_pos_w_haggr = wtmean(inv_mu_sepcal_TL_sFSQ_t1_pos_w), weight( revShare )
		capture drop temp
		gen mu_TL_sFSQ_t1_pos_w_haggr = 1/inv_mu_TL_sFSQ_t1_pos_w_haggr
		gen  l_mu_TL_sFSQ_t1_pos_w_haggr = log(mu_TL_sFSQ_t1_pos_w_haggr)
		
		




*************** 6.2 Compute the sector-level HHI

	gen share5_sq = share5*share5
	bysort year naf_single: egen hhi = total(share5_sq), missing
	
	su share5_sq  hhi 

******************************************************************************************
*save the firm-level database
save BCG/data/full_firm_baseline_${winlevel_gl}.dta, replace
******************************************************************************************

*************** 6.3 Construct Sector-Level Panel and merge with Sector-level data	


*Keep one observation by sector*year
	duplicates drop naf_single_num  year, force 

*save/USE the data
	save BCG/data/full_sec_baseline_${winlevel_gl}.dta, replace
	
	use BCG/data/full_sec_baseline_${winlevel_gl}.dta, clear

*set up the panel
	xtset naf_single_num year
	
	

*************** 6.4 Markup - HHI regressions

***First-Difference
		
		******set up panel
		xtset naf_single_num year
					
		******Compute First-Difference
			gen FDinv_mu_TL_sFSQ_t1_pos_w_h = inv_mu_TL_sFSQ_t1_pos_w_h - L.inv_mu_TL_sFSQ_t1_pos_w_h
			gen FDhhi = hhi - L.hhi
			
			local top_tr $toptr_gl
			local bot_tr $bottr_gl
			local winlevel $winlevel_gl
							
			winsor  FDinv_mu_TL_sFSQ_t1_pos_w_h , p(.`winlevel') gen(FDinv_mu_TL_sFSQ_t1_pos_w_h_w)

			**Some summarry stats
			su FDinv_mu_TL_sFSQ_t1_pos_w_h FDinv_mu_TL_sFSQ_t1_pos_w_h_w FDhhi   
			
		 ******Regressions
			reghdfe FDinv_mu_TL_sFSQ_t1_pos_w_h  FDhhi  if year>1994   , noabsorb  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , replace tex   keep(FDhhi) addtext(Sector FE, No, Year FE, No, Cluster , Sector  Year)

			reghdfe FDinv_mu_TL_sFSQ_t1_pos_w_h  FDhhi   i.year if year>1994 , noabsorb  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , append tex   keep(FDhhi) addtext(Sector FE, No, Year FE, Yes, Cluster , Sector Year)
			
			reghdfe FDinv_mu_TL_sFSQ_t1_pos_w_h  FDhhi  if year>1994   , absorb(naf2d_num#year)  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , append tex   keep(FDhhi) addtext(Sector FE, No, Year FE, No, Sector2d* Year FE, Yes, Cluster , Sector  Year)
	
	
	
***Level
		******Regressions
			reghdfe inv_mu_TL_sFSQ_t1_pos_w_h  hhi  if year>1994   , noabsorb  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_level_$winlevel_gl$specsDeta , replace tex   keep(hhi) addtext(Sector FE, No, Year FE, No, Cluster , Sector  Year, Sample, Pos, RHS, Max, Drop 94, Yes)
			*
			reghdfe inv_mu_TL_sFSQ_t1_pos_w_h  hhi   if year>1994 , absorb(year)  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_level_$winlevel_gl$specsDeta , append tex   keep(hhi) addtext(Sector FE, No, Year FE, Yes, Cluster , Sector Year, Sample, Pos, RHS, Max, Drop 94, Yes)
			
			reghdfe inv_mu_TL_sFSQ_t1_pos_w_h  hhi   if year>1994 , absorb(naf_single_num)  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_level_$winlevel_gl$specsDeta , append tex   keep(hhi) addtext(Sector FE, Yes, Year FE, No, Cluster , Sector Year, Sample, Pos, RHS, Max, Drop 94, Yes)
			
			reghdfe inv_mu_TL_sFSQ_t1_pos_w_h  hhi   i.year if year>1994 , absorb(naf_single_num)  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_level_$winlevel_gl$specsDeta , append tex   keep(hhi) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector Year, Sample, Pos, RHS, Max, Drop 94, Yes)
			*
			
			reghdfe inv_mu_TL_sFSQ_t1_pos_w_h  hhi   if year>1994 , absorb(naf2d_num#year)  cl(year naf_single_num)
			outreg2 using BCG\results_sl\MarkupHhi_level_$winlevel_gl$specsDeta , append tex   keep(hhi) addtext(Sector FE, No, Year FE, No, Sector 2D*year, Yes , Cluster , Sector Year, Sample, Pos, RHS, Max, Drop 94, Yes)
			*
			




*************** 6.5 Markup - Sector-Output 		
	
	***FD (baseline 1)
		reghdfe d.l_mu_TL_sFSQ_t1_pos_w_h  d.l_va_total   i.year  if year>1994, absorb(naf_single_num)  cl(naf_single_num)
	outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(d.l_va_total) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  )
	
	**** With Hamilton filter (baseline 2)
		***set up panel
		xtset naf_single_num year

		***Hamilton log-markup
		hamiltonfilter l_mu_TL_sFSQ_t1_pos_w_h, frequency(yearly) stub(lmuTLsFSQt1poswh_ha) 
		gen l_muTLsFSQt1_pos_w_h_ha_dev = l_mu_TL_sFSQ_t1_pos_w_h - lmuTLsFSQt1poswh_ha_trend
		
		***Hamilton log-output
		capture drop l_va_total_ha*
		hamiltonfilter l_va_total, frequency(yearly) stub(l_va_total_ha) 
		gen l_va_total_ha_dev = l_va_total - l_va_total_ha_trend
			

		****Regressions
		reghdfe l_muTLsFSQt1_pos_w_h_ha_dev  l_va_total_ha_dev   i.year  , absorb(naf_single_num)  cl(naf_single_num)
		outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  )
			

	
	***In level (robustness 1)
		reghdfe l_mu_TL_sFSQ_t1_pos_w_h  l_va_total   i.year  if year>1994  , absorb(naf_single_num)  cl(naf_single_num)
		outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  )
	
		
	***With HP deviations  (robustness 2)
		***set up panel
		xtset naf_single_num year
	
		***HP-filtered log-markup
		capture drop x
		tsfilter hp x = l_mu_TL_sFSQ_t1_pos_w_h, smooth(6.25) trend(l_mu_TL_sFSQ_t1_pos_w_h_hp)
		drop x
		gen l_muTLsFSQt1_pos_w_h_hp_dev = l_mu_TL_sFSQ_t1_pos_w_h - l_mu_TL_sFSQ_t1_pos_w_h_hp
		

		****Regressions
		reghdfe l_muTLsFSQt1_pos_w_h_hp_dev  l_va_total_hp_dev   i.year  if year>1994  , absorb(naf_single_num)  cl(naf_single_num)
		outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  )
		

		
					
*************** 6.6 HHI - Sector-Output 	

	****taking logs
		gen l_hhi = log(hhi)
		gen FD_l_hhi = d.l_hhi
		
		
	****First-Difference (baseline 1)
		reghdfe FD_l_hhi l_va_total_FD  i.year if year>1994   ,  absorb(naf_single_num)  cl( naf_single_num)
		outreg2 using BCG\results_sl\HHI_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(l_va_total_FD) addtext(Sector FE, Yes, Year FE, Yes, Sector-Year FE, No, Cluster ,  Sector)
		
	****Hamilton (baseline 2)
		***Hamilton log-hhi
		capture drop l_hhi_ha*
		hamiltonfilter l_hhi, frequency(yearly) stub(l_hhi_ha) 
		gen l_hhi_ha_dev = l_hhi - l_hhi_ha_trend
		
		**regression
			reghdfe l_hhi_ha_dev l_va_total_ha_dev  i.year if year>1994   ,  absorb(naf_single_num)  cl( naf_single_num)
			outreg2 using BCG\results_sl\HHI_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_ha_dev) addtext(Sector FE, Yes, Year FE, Yes, Sector-Year FE, No, Cluster ,  Sector)
	
	****Level (robust 1)
		reghdfe l_hhi l_va_total  i.year if year>1994   ,  absorb(naf_single_num)  cl( naf_single_num)
		outreg2 using BCG\results_sl\HHI_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total) addtext(Sector FE, Yes, Year FE, Yes, Sector-Year FE, No, Cluster ,  Sector)
		
	**** HP (robust 2)
		***HP-dev log-hhi
			***set up panel
			xtset naf_single_num year
		
			***HP-filtered log-markup
			capture drop x
			tsfilter hp x = l_hhi, smooth(6.25) trend(l_hhi_hp)
			drop x
			gen l_hhi_hp_dev = l_hhi - l_hhi_hp
			
			**regression
			reghdfe l_hhi_hp_dev l_va_total_hp_dev  i.year if year>1994   ,  absorb(naf_single_num)  cl( naf_single_num)
			outreg2 using BCG\results_sl\HHI_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_hp_dev) addtext(Sector FE, Yes, Year FE, Yes, Sector-Year FE, No, Cluster ,  Sector)
		
	
	
	
*************** 6.7 Markup - Aggregate-Output (Bils-Klenow-Malin)

*** Weight variable
	bysort naf_single_num: egen weightvar_va_total = mean(va_total)

*** First-Difference (baseline 1)
	****Compute aggregate output growth rate (log FD )
	xtset naf_single_num year
	capture gen FD_l_va_agg = l_va_agg - L.l_va_agg
	
	gen FD_l_mu_TL_sFSQ_t1_pos_w_h = l_mu_TL_sFSQ_t1_pos_w_h - L.l_mu_TL_sFSQ_t1_pos_w_h
	
	***** Regression
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_h  FD_l_va_agg  [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)	
	outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , replace tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Sectors)
			
*** Hamilton (baseline 2)
	***set up panel
	xtset naf_single_num year
		
	***Hamilton log-markup
	capture drop lmuTLsFSQt1poswh_ha* l_muTLsFSQt1_pos_w_h_ha_dev
	hamiltonfilter l_mu_TL_sFSQ_t1_pos_w_h, frequency(yearly) stub(lmuTLsFSQt1poswh_ha) 
	gen l_muTLsFSQt1_pos_w_h_ha_dev = l_mu_TL_sFSQ_t1_pos_w_h - lmuTLsFSQt1poswh_ha_trend
			
	***Hamilton log-output
	capture drop  l_va_agg_ha*
	hamiltonfilter  l_va_agg, frequency(yearly) stub( l_va_agg_ha) 
	gen l_va_agg_ha_dev = l_va_agg - l_va_agg_ha_trend

	*** Regression				
	reghdfe l_muTLsFSQt1_pos_w_h_ha_dev  l_va_agg_ha_dev  [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)	
	outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_agg_ha_dev ) addtext(Sector FE, Yes, Cluster , Sector)
		
*** HP-Filtered (Robust)
		***set up panel
		xtset naf_single_num year
	
		***HP-filtered log-markup
		capture drop l_mu_TL_sFSQ_t1_pos_w_h_hp
		capture drop l_muTLsFSQt1_pos_w_h_hp_dev
		
		tsfilter hp x = l_mu_TL_sFSQ_t1_pos_w_h, smooth(6.25) trend(l_mu_TL_sFSQ_t1_pos_w_h_hp)
		drop x
		gen l_muTLsFSQt1_pos_w_h_hp_dev = l_mu_TL_sFSQ_t1_pos_w_h - l_mu_TL_sFSQ_t1_pos_w_h_hp
		
		***Regression
		reghdfe l_muTLsFSQt1_pos_w_h_hp_dev  l_va_agg_hp_dev  [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)	
		outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_agg_hp_dev ) addtext(Sector FE, Yes, Cluster , Sector)
			



************************************************************************************************************************************************************************************
******************************************************************* 7. Aggregate-Markup Statistiques
***************************************************************************************************************************************************

*************** 7.0 Preambuled, starting fom the firm-level data

	*start from the firm-level data
	use BCG/data/full_firm_baseline_${winlevel_gl}.dta, clear
		
	*Keep one observation by year
		duplicates drop year, force 

	*save/USE the data
		save BCG/data/full_aggr_baseline_${winlevel_gl}.dta, replace
		use BCG/data/full_aggr_baseline_${winlevel_gl}.dta, clear

	*set up the panel
	tset year
		
	*Summary stats on aggregate markup
	su mu_TL_sFSQ_t1_pos_w_haggr
	su  l_mu_TL_sFSQ_t1_pos_w_haggr 	

	

*************** 7.1 Filtered out the aggregate series (HP-filtered, Hamilton,  FD)

	*rename the aggregate markup variables because stata is terrible
		** Aggregate markup computed for positive markup only but revenue share on full sample
		rename l_mu_TL_sFSQ_t1_pos_w_haggr LmuTLsFSQt1poswhaggr		
		
		
		
	*Compute deviation from HP trend 
	foreach l_aggre_mu in  LmuTLsFSQt1poswhaggr  {
		di "`l_aggre_mu '"
		**fix gaps (hp filter does not run with gaps but now it should no be any of these gaps)
		drop if `l_aggre_mu' == .  //0 changes

		** generate HP-filtered data 
		tsfilter hp x = `l_aggre_mu', smooth(6.25) trend(`l_aggre_mu'_hp)
		drop x
		gen `l_aggre_mu'_hpdev = `l_aggre_mu' - `l_aggre_mu'_hp
		
	}
	
	*Compute deviation from Hamilton trend 
	foreach l_aggre_mu in  LmuTLsFSQt1poswhaggr   {
		di "`l_aggre_mu '"
		**fix gaps (hp filter does not run with gaps but now it should no be any of these gaps)
		drop if `l_aggre_mu' == .  //0 changes

		** generate Hamilton-filtered data 
		hamiltonfilter `l_aggre_mu', frequency(yearly) stub(`l_aggre_mu'_ha) 
		gen `l_aggre_mu'_hadev = `l_aggre_mu' - `l_aggre_mu'_ha_trend
		
		
	}
	
	**for aggregate output
	capture drop l_va_agg_ha*
	hamiltonfilter l_va_agg, frequency(yearly) stub(l_va_agg_ha) 
	gen l_va_agg_ha_dev = l_va_agg - l_va_agg_ha_trend
		
			
	
	
	*Compute Fist Difference of Log
		**set up panel
		tset year
		
		**compute log first differennce
		foreach l_aggre_mu in  LmuTLsFSQt1poswhaggr   {
				
				gen FD`l_aggre_mu'  = `l_aggre_mu' - L.`l_aggre_mu'
		}
		
		
		foreach var in l_va_agg  {
				capture drop FD`var'
				gen FD`var' = (`var' - L.`var')
		}
		
	

*************** 7.2 The statistics: markup volatility, aggregate va vol, and, corr(markup,l_va)

*Stats for Hamilton filtered (basleine 1)
	su l_va_agg_ha_dev LmuTLsFSQt1poswhaggr_hadev if year>1994 //relative vol: 0.
	pwcorr l_va_agg_ha_dev LmuTLsFSQt1poswhaggr_hadev if year>1994, sig obs //corr = 0.  (pval = 0.)
			
*Stats for log FD (baseline 2)
	su FDl_va_agg FDLmuTLsFSQt1poswhaggr if year>1994 //relative vol: 0.
	pwcorr FDl_va_agg FDLmuTLsFSQt1poswhaggr if year>1994, sig obs //corr = 0.  (pval = 0.)

*Stats for HP filtered (robust)
	su l_va_agg_hp_dev LmuTLsFSQt1poswhaggr_hpdev if year>1994 //relative vol: 0.
	pwcorr l_va_agg_hp_dev LmuTLsFSQt1poswhaggr_hpdev if year>1994, sig obs //corr = 0.  (pval = 0.)


			
			
***************************************************************************************************************************************************
******************************************************************* 8. Within-Between
***************************************************************************************************************************************************


	*start from the firm-level data
	use BCG/data/full_firm_baseline_${winlevel_gl}.dta, clear
	
	
*************** 8.1 Sector-Level Decomposition
	xtset firmsId year

	*Compute change in sector markup within term
		**average market share over two periods
		gen avg2per_share5 = 0.5*(share5 +l.share5)
		
		**Compute inverse markup change 
			**Compute inverse markup change 
			gen FDInvMuTLsFSQ_t1_pos_w=inv_mu_sepcal_TL_sFSQ_t1_pos_w-L.inv_mu_sepcal_TL_sFSQ_t1_pos_w
			
		
		**Compute within term 
		gen temp_within = avg2per_share5*FDInvMuTLsFSQ_t1_pos_w
		bysort naf_single year: egen invMuTLsFSQt1Pos_w_within = total(temp_within)
		
		
		**Move to sector-level panel and perform regression
		preserve
			***Keep one observation by sector, year
			duplicates drop naf_single year, force 
			
			***set up panel
			xtset naf_single_num year
			
			***Produce sector-level inverse markup change 
			gen inv_mu_TL_sFSQ_t1_pos_w_h_change = inv_mu_TL_sFSQ_t1_pos_w_h - L.inv_mu_TL_sFSQ_t1_pos_w_h
			
			***Produce reallocation term
			gen invMuTLsFSQt1Pos_w_realloc = inv_mu_TL_sFSQ_t1_pos_w_h_change - invMuTLsFSQt1Pos_w_within
			
			***Running the regression and saving coeffiecents
			statsby, by(naf_single) saving("BCG\results_sl\coeff_within_sector${specsDeta}_${winlevel_gl}.dta", replace): reg invMuTLsFSQt1Pos_w_within inv_mu_TL_sFSQ_t1_pos_w_h_change
			
			***Show results
			use "BCG\results_sl\coeff_within_sector${specsDeta}_${winlevel_gl}.dta", clear
			su, d
			
		 restore
	
	
	
* clean up 
clear all 

	

